from sklearn.datasets import fetch_lfw_people
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt

lfw = fetch_lfw_people(min_faces_per_person=70, resize=0.4)
X = lfw.data
n_samples, h, w = lfw.images.shape

def plot_gallery(images, height, width, n_row, n_col, cmap=plt.cm.gray):
    fig, axs = plt.subplots(n_row,n_col, figsize=(14,9),
                            subplot_kw={'xticks':(), 'yticks':()})
    for i, image in zip(range(n_row*n_col), images):
        r = int(i/n_col); c = i%n_col
        axs[r,c].imshow(image.reshape((h, w)), cmap=cmap) 

pca = PCA(n_components=18, svd_solver='randomized').fit(X) 

print(pca.components_.shape)
plot_gallery(pca.components_, h, w, 3, 6)
plt.show()
